import Q from './Q'
const za = function () {
  return function (e, t) {
    if (Array.isArray(e))
      return e;
    if (Symbol.iterator in Object(e))
      return function (e, t) {
        var n = []
          , o = !0
          , r = !1
          , i = undefined;
        try {
          for (var a, s = e[Symbol.iterator](); !(o = (a = s.next()).done) && (n.push(a.value),
            !t || n.length !== t); o = !0)
            ;
        } catch (l) {
          r = !0,
            i = l
        } finally {
          try {
            !o && s["return"] && s["return"]()
          } finally {
            if (r)
              throw i
          }
        }
        return n
      }(e, t);
    throw new TypeError("Invalid attempt to destructure non-iterable instance")
  }
}()
// colIndex rowIndex  col/row remove/create  hotInstance
const handleColRow = (e, t, n, o, r, handsontableHelper) => {
  window.isHandsonTableEdited = !0;
  let i = r.getSettings()
    , a = i.cellWithField
    , s = i.cellWithImg
    , l = {}
    , u = {};
  let cellWithQRcode = i.cellWithQRcode
  let newCellWithQRcode = {}
  let cellWithBRcode = i.cellWithBRcode
  let newCellWithBRcode = {}
  if ("row" === n) {
    if ("create" === o) {
      for (let c in a)
        if (a.hasOwnProperty(c)) {
          let d = c.split(":")
            , h = za(d, 2)
            , f = h[0]
            , p = h[1];
          f >= e ? l[parseInt(f) + t + ":" + p] = a[c] : l[c] = a[c]
        }
      for (let m in s)
        if (s.hasOwnProperty(m)) {
          let g = m.split(":")
            , y = za(g, 2)
            , v = y[0]
            , w = y[1];
          v >= e ? u[parseInt(v) + t + ":" + w] = s[m] : u[m] = s[m]
        }
      for (let pos in cellWithQRcode) {
        if (cellWithQRcode.hasOwnProperty(pos)) {
          let d = pos.split(":")
            , h = za(d, 2)
            , f = h[0]
            , p = h[1];
          f >= e ? newCellWithQRcode[parseInt(f) + t + ":" + p] = cellWithQRcode[pos]
            : newCellWithQRcode[pos] = cellWithQRcode[pos]
        }
      }

      for (let pos in cellWithBRcode) {
        if (cellWithBRcode.hasOwnProperty(pos)) {
          let d = pos.split(":")
            , h = za(d, 2)
            , f = h[0]
            , p = h[1];
          f >= e ? newCellWithBRcode[parseInt(f) + t + ":" + p] = cellWithBRcode[pos]
            : newCellWithBRcode[pos] = cellWithBRcode[pos]
        }
      }

    } else {
      for (var _ in a)
        if (a.hasOwnProperty(_)) {
          var b = _.split(":")
            , C = za(b, 2)
            , T = C[0]
            , M = C[1];
          T >= e + t ? l[parseInt(T) - t + ":" + M] = a[_] : T < e && (l[_] = a[_])
        }
      for (var S in s)
        if (s.hasOwnProperty(S)) {
          var k = S.split(":")
            , E = za(k, 2)
            , O = E[0]
            , L = E[1];
          O >= e + t ? u[parseInt(O) - t + ":" + L] = s[S] : O < e && (u[S] = s[S])
        }
      for (let pos in cellWithQRcode) {
        if (cellWithQRcode.hasOwnProperty(pos)) {
          let d = pos.split(":")
            , h = za(d, 2)
            , f = h[0]
            , p = h[1];
          f >= e + t ? newCellWithQRcode[parseInt(f) - t + ":" + p] = cellWithQRcode[pos]
            : f < e && (newCellWithQRcode[pos] = cellWithQRcode[pos])
        }
      }

      for (let pos in cellWithBRcode) {
        if (cellWithBRcode.hasOwnProperty(pos)) {
          let d = pos.split(":")
            , h = za(d, 2)
            , f = h[0]
            , p = h[1];
          f >= e + t ? newCellWithBRcode[parseInt(f) - t + ":" + p] = cellWithBRcode[pos]
            : f < e && (newCellWithBRcode[pos] = cellWithBRcode[pos])
        }
      }
    }
  } else if ("create" === o) {
    for (var D in a)
      if (a.hasOwnProperty(D)) {
        var x = D.split(":")
          , R = za(x, 2)
          , N = R[0]
          , P = R[1];
        P >= e ? l[N + ":" + (parseInt(P) + t)] = a[D] : l[D] = a[D]
      }
    for (var A in s)
      if (s.hasOwnProperty(A)) {
        var H = A.split(":")
          , j = za(H, 2)
          , Y = j[0]
          , I = j[1];
        I >= e ? u[Y + ":" + (parseInt(I) + t)] = s[A] : u[A] = s[A]
      }
    for (let A in cellWithQRcode) {
      if (cellWithQRcode.hasOwnProperty(A)) {
        let H = A.split(":")
          , j = za(H, 2)
          , Y = j[0]
          , I = j[1];
        I >= e ? newCellWithQRcode[Y + ":" + (parseInt(I) + t)] = cellWithQRcode[A] : newCellWithQRcode[A] = cellWithQRcode[A]
      }
    }
    for (let A in cellWithBRcode) {
      if (cellWithBRcode.hasOwnProperty(A)) {
        let H = A.split(":")
          , j = za(H, 2)
          , Y = j[0]
          , I = j[1];
        I >= e ? newCellWithBRcode[Y + ":" + (parseInt(I) + t)] = cellWithBRcode[A] : newCellWithBRcode[A] = cellWithBRcode[A]
      }
    }
  } else {
    for (var F in a)
      if (a.hasOwnProperty(F)) {
        var W = F.split(":")
          , B = za(W, 2)
          , z = B[0]
          , V = B[1];
        V >= e + t ? l[z + ":" + (parseInt(V) - t)] = a[F] : V < e && (l[F] = a[F])
      }
    for (var U in s)
      if (s.hasOwnProperty(U)) {
        var G = U.split(":")
          , K = za(G, 2)
          , X = K[0]
          , q = K[1];
        q >= e + t ? u[X + ":" + (parseInt(q) - t)] = s[U] : q < e && (u[U] = s[U])
      }
    for (let U in cellWithQRcode) {
      if (cellWithQRcode.hasOwnProperty(U)) {
        var G = U.split(":")
          , K = za(G, 2)
          , X = K[0]
          , q = K[1];
        q >= e + t ? newCellWithQRcode[X + ":" + (parseInt(q) - t)] = cellWithQRcode[U] : q < e && (newCellWithQRcode[U] = cellWithQRcode[U])
      }
    }
    for (let U in cellWithBRcode) {
      if (cellWithBRcode.hasOwnProperty(U)) {
        var G = U.split(":")
          , K = za(G, 2)
          , X = K[0]
          , q = K[1];
        q >= e + t ? newCellWithQRcode[X + ":" + (parseInt(q) - t)] = cellWithBRcode[U] : q < e && (newCellWithQRcode[U] = cellWithBRcode[U])
      }
    }
  }
  var J = {
    // cellData: function (e, t, n, o, r, handsontableHelper) {
    //   var cellDatas = []
    //   const maxRow = r.getSettings().contentEdge.maxRow
    //   const maxCol = r.getSettings().contentEdge.maxCol
    //   for (let row = 0; row < maxRow; row++) {
    //     for (let col = 0; col < maxCol; col++) {
    //       var data = r.getDataAtCell(row, col)
    //         , expression = handsontableHelper.getCellExpression(row, col)
    //         , img = handsontableHelper.getCellWithImg(row, col)
    //         , qr = handsontableHelper.getCellWithQRcode(row, col)
    //         , br = handsontableHelper.getCellWithBRcode(row, col);
    //       var p = {}
    //       if (o === 'creat') {

    //       } else {

    //       }
    //       p.data = data || expression || img || qr || br
    //       p.row = row
    //       p.col = col
    //       cellDatas.push([row, col, data || null])
    //     }
    //   }
    //   return cellDatas.length ? cellDatas : null
    // }(e, t, n, o, r, handsontableHelper),
    mergeCells: function (e, t, n, o, r, handsontableHelper) {
      var i = (r.getSettings().mergeCells || []).slice();
      i = "row" === n ? "create" === o ? i.map(function (n) {
        return n.row >= e ? Object.assign({}, n, {
          row: n.row + t
        }) : n.row + n.rowspan > e ? Object.assign({}, n, {
          rowspan: n.rowspan + t
        }) : Object.assign({}, n)
      }) : (i = i.filter(function (n) {
        return !(n.row >= e && n.row <= e + t - 1 && n.row + n.rowspan <= e + t)
      })).map(function (n) {
        if (n.row + n.rowspan <= e)
          return Object.assign({}, n);
        if (n.row < e && n.row + n.rowspan > e) {
          var o = e - n.row
            , r = n.rowspan - o - t;
          return Object.assign({}, n, {
            rowspan: r >= 0 ? r + o : o
          })
        }
        return n.row < e + t ? Object.assign({}, n, {
          row: e,
          rowspan: n.rowspan - (e + t - n.row)
        }) : Object.assign({}, n, {
          row: n.row - t
        })
      }) : "create" === o ? i.map(function (n) {
        return n.col >= e ? Object.assign({}, n, {
          col: n.col + t
        }) : n.col + n.colspan > e ? Object.assign({}, n, {
          colspan: n.colspan + t
        }) : Object.assign({}, n)
      }) : (i = i.filter(function (n) {
        return !(n.col >= e && n.col <= e + t - 1 && n.col + n.colspan <= e + t)
      })).map(function (n) {
        if (n.col + n.colspan <= e)
          return Object.assign({}, n);
        if (n.col < e && n.col + n.colspan > e) {
          var o = e - n.col
            , r = n.colspan - o - t;
          return Object.assign({}, n, {
            colspan: r >= 0 ? r + o : o
          })
        }
        return n.col < e + t ? Object.assign({}, n, {
          col: e,
          colspan: n.colspan - (e + t - n.col)
        }) : Object.assign({}, n, {
          col: n.col - t
        })
      });
      return i.filter(function (e) {
        return e.row >= 0 && e.col >= 0 && !(e.rowspan <= 1 && e.colspan <= 1)
      })
    }(e, t, n, o, r, handsontableHelper),
    cellFields: l,
    cellImgs: u,
    cellWithQRcode: newCellWithQRcode,
    cellWithBRcode: newCellWithBRcode,
    tableStructure: function (e, t, n, o, r) {
      var i = {};
      "row" === n ? i.rowhs = {
        index: e,
        amount: t,
        op: o
      } : i.colws = {
        index: e,
        amount: t,
        op: o
      };
      return i
    }(e, t, n, o)
  }
  var $ = function (e, t, n, o, r, handsontableHelper) {
    if ("remove" !== o)
      return null;
    var i = []
      , a = null
      , s = null
      , l = null
      , u = null;
    "row" === n ? (a = e,
      s = 0,
      u = r.countCols(),
      l = e + t) : (a = 0,
        s = e,
        u = e + t,
        l = r.countRows());
    for (var c = a; c < l; c++)
      for (var d = s; d < u; d++) {
        var h = r.getCellMeta(c, d);
        if (h.className && "" !== h.className.trim() || h.style && 0 !== Object.keys(h.style).length) {
          var replaceCell = "row" === n ? r.getCellMeta(c + t, d) : r.getCellMeta(c, d + t);
          i.push({
            row: c,
            col: d,
            border: replaceCell && replaceCell.border || {},
            style: replaceCell && replaceCell.style || {},
            className: replaceCell && replaceCell.className || ""
          })
        }
      }
    return i
  }(e, t, n, o, r, handsontableHelper);
  $ && $.length > 0 && (J.cellMetas = $)
  return new Q(J, r).diff()
}


export function beforeRemoveCol(e, t, n, o, handsontableHelper) {
  return handleColRow(e, t, "col", "remove", o, handsontableHelper)
}

export function beforeRemoveRow(e, t, n, o, handsontableHelper) {
  return handleColRow(e, t, "row", "remove", o, handsontableHelper)
}

export function afterCreateCol(e, t, n, o, handsontableHelper) {
  return handleColRow(e, t, "col", "create", o, handsontableHelper)
}

export function afterCreateRow(e, t, n, o, handsontableHelper) {
  return handleColRow(e, t, "row", "create", o, handsontableHelper)
}